package views;

import java.util.Locale;

import model.EnhancedCostEvaluator;
import model.Problem;
import model.Solution;
import solver.MathSolverLC_CV_Final;
import util.Chronograph;
import util.Importer;
import util.SolutionImporter;

public class MachineReassignment {

	static final String TEAM_ID = "S5";
	
	static {
//		System.load("/home/chgogos/or-tools-read-only/libjnilinearsolver.so");
		 System.loadLibrary("jnilinearsolver");
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.ENGLISH);
		int time_limit;
		String instance_filename;
		String original_solution_filename;
		String new_solution_filename;
		long seed;
		if ((args.length == 1) && (args[0].equalsIgnoreCase("-name"))) {
			System.out.printf("%s", TEAM_ID);
		} else if (args.length == 11) {
			time_limit = Integer.parseInt(getArgumentValue(args, "-t"));
			instance_filename = getArgumentValue(args, "-p");
			original_solution_filename = getArgumentValue(args, "-i");
			new_solution_filename = getArgumentValue(args, "-o");
			seed = Long.parseLong(getArgumentValue(args, "-s"));
			MachineReassignment app = new MachineReassignment();
			app.launch(time_limit, instance_filename,
					original_solution_filename, new_solution_filename, seed);
		} else {
			System.out.println("Usage:");
			System.out.println("java -jar MachineReassignment -name");
			System.out.println();
			System.out.println("java -jar MachineReassignment "
					+ "-t time_limit " + "-p instance_filename "
					+ "-i original_solution_filename "
					+ "-o new_solution_filename " + "-name " + "-s seed");
		}
	}

	private void launch(int time_limit, String instance_filename,
			String original_solution_filename, String new_solution_filename,
			long seed) {
		Chronograph chronos_test = new Chronograph();
		chronos_test.reset();
		// System.out.printf("\ntime_limit=%d\n", time_limit);
		// System.out.printf("problem_instance_filename=%s\n",
		// instance_filename);
		// System.out.printf("original_solution_filename=%s\n",
		// original_solution_filename);
		// System.out.printf("new_solution_filename=%s\n",
		// new_solution_filename);
		// System.out.printf("team=%s\n", TEAM_ID);
		// System.out.printf("seed=%d\n", seed);

		Chronograph chronos = new Chronograph();
		chronos.reset();
		Importer importer = new Importer(instance_filename);
		importer.readData();
		Problem problem = importer.getProblem();
		Problem.VERBOSE = false;
		// System.out.println(problem.getCondensedInfo());
		SolutionImporter solutionImporter = new SolutionImporter(problem);
		solutionImporter.setSolutionFileName(original_solution_filename);
		Solution initial_solution = solutionImporter.readData();

		Problem.RANDOM_SEED = seed;
		boolean useGRB = true;
		MathSolverLC_CV_Final solver = new MathSolverLC_CV_Final(problem,
				initial_solution, useGRB);

		chronos.lap();
		int time_passed_sec = chronos.getElapsedTimeInSeconds();
		solver.setSaved_solution_filename(new_solution_filename);
		solver.solveWithTimeLimit(time_limit - time_passed_sec);

		 Solution final_solution = solver.getCurrentSolution();
		 EnhancedCostEvaluator ece = new EnhancedCostEvaluator();
		 ece.setSolutionPair(initial_solution, final_solution);
		 ece.computeCost();
		 ece.printCostAnalysis();
		 boolean f = ece.isFeasible();
		 chronos_test.lap();
		 System.out.printf("Using %s, time passed=%d cost=%,d %s\n",
		 useGRB ? "GUROBI" : "CBC",
		 chronos_test.getElapsedTimeInSeconds(),
		 ece.getNewSolutionCost(), f ? ":)" : ":(");
		 solver.saveSolution(ece.getNewSolutionCost(),
		 chronos_test.getElapsedTimeInSeconds(), true);
	}

	private static String getArgumentValue(String[] args, String key) {
		if (key.equalsIgnoreCase("-name"))
			return TEAM_ID;
		int pos = 0;
		for (int i = 0; i < args.length; i++) {
			if (args[i].equalsIgnoreCase(key)) {
				pos = i;
			}
		}
		return args[pos + 1];
	}

	private void saveSolution(String fn, Solution new_solution) {
		new_solution.exportToFileName(fn);
		System.out.println("File " + fn + " saved!");
	}

}
